package com.yjt.fresh.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyOrderDetailChild;
import com.yjt.fresh.api.ApiCancelOrder;
import com.yjt.fresh.api.ApiConfirmReceive;
import com.yjt.fresh.api.ApiDeleteOrder;
import com.yjt.fresh.api.ApiGetOrderDetail;
import com.yjt.fresh.api.ApiNoticeSendGoods;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanOrder;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.utils.TimeUtils;
import com.yjt.fresh.widget.CustomToastResult;
import com.yjt.fresh.widget.ScrollLinearLayoutManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by UPC on 2018/8/11.
 */

public class AtyOrderDetail extends BaseSimpleActivity {
    private static final int REQUEST_CODE = 1008;
    @BindView(R.id.rlBack)
    RelativeLayout mRlBack;
    @BindView(R.id.tvAppAtyOrderDetailOrderNum)
    TextView mTvOrderNum;
    @BindView(R.id.tvAppAtyOrderDetailOrderStatus)
    TextView mTvOrderStatus;
    @BindView(R.id.tvAppAtyOrderDetailOrderStatusDetail)
    TextView mTvStatusDetail;
    @BindView(R.id.rlAppAtyOrderDetailCheckExpress)
    RelativeLayout mRlCheckExpress;
    @BindView(R.id.tvAppAtyOrderDetailReceiveName)
    TextView mTvReceiveName;
    @BindView(R.id.tvAppAtyOrderDetailReceivePhone)
    TextView mTvReceivePhone;
    @BindView(R.id.tvAppAtyOrderDetailAddress)
    TextView mTvAddress;
    @BindView(R.id.rvAppAtyOrderDetailChild)
    RecyclerView mRvChild;
    @BindView(R.id.tvAppAtyOrderDetailPayWay)
    TextView mTvPayWay;
    @BindView(R.id.rlAppAtyOrderDetailPayWay)
    RelativeLayout mRlPayWay;
    @BindView(R.id.tvAppAtyOrderDetailTotalMoney)
    TextView mTvTotalMoney;
    @BindView(R.id.tvAppAtyOrderDetailCouponMoney)
    TextView mTvCouponMoney;
    @BindView(R.id.tvAppAtyOrderDetailNeedMoney)
    TextView mTvNeedMoney;
    @BindView(R.id.tvAppAtyOrderDetailContactService)
    TextView mTvService;
    @BindView(R.id.tvAppAtyOrderDetailCancelOrder)
    TextView mTvCancelOrder;
    @BindView(R.id.tvAppAtyOrderDetailNoticeSend)
    TextView mTvRemindSend;
    @BindView(R.id.tvAppAtyOrderDetailCheckExpress)
    TextView mTvCheckExpress;
    @BindView(R.id.tvAppAtyOrderDetailConfirmReceive)
    TextView mTvConfirmReceive;
    @BindView(R.id.tvAppAtyOrderDetailService)
    TextView mTvAfterSale;
    @BindView(R.id.tvAppAtyOrderDetailComment)
    TextView mTvComment;
    @BindView(R.id.tvAppAtyOrderDetailDeleteOrder)
    TextView mTvDeleteOrder;
    @BindView(R.id.tvAppAtyOrderDetailPay)
    TextView mTvPay;
    @BindView(R.id.rlAppAtyOrderDetailRestTime)
    RelativeLayout mRlRestTime;
    @BindView(R.id.tvAppAtyOrderDetailRestTime)
    TextView mTvRestTime;
    @BindView(R.id.tvAppAtyOrderDetailAfterSaleAbove)
    TextView mTvAfterSaleAbove;

    private AdpRvAtyOrderDetailChild mAdpChild;
    private String mOrderID;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_order_detail;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        mOrderID = getIntent().getStringExtra(MyConstants.ORDER_ID);
        mRvChild.setFocusable(false);
        initRv();
        getData();
        initClick();
    }

    private void initClick() {
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRlCheckExpress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkExpress();
            }
        });

        mAdpChild.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, AtyProductDetail.class);
                intent.putExtra(MyConstants.GOODS_ID, mAdpChild.getData().get(position).getGOODS_ID());
                startActivity(intent);
            }
        });

        mTvService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contactService();
            }
        });

        mTvCancelOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(mContext, R.style.MyDialog);
                dialog.setContentView(R.layout.app_dialog_sure_cancel);
                TextView tvContent = dialog.findViewById(R.id.tvAppDIalogSureCancelContent);
                TextView tvCancel = dialog.findViewById(R.id.tvAppDialogSureCancelCancel);
                TextView tvSure = dialog.findViewById(R.id.tvAppDialogSureCancelSure);
                tvContent.setText("确定要取消订单吗?");
                tvCancel.setText("取消");
                tvSure.setText("确定");
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                });
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cancelOrder();
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                });
                dialog.show();
            }
        });

        mTvRemindSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                noticeSend();
            }
        });

        mTvCheckExpress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkExpress();
            }
        });

        mTvConfirmReceive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmReceive();
            }
        });

        mTvAfterSale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                afterSale();
            }
        });

        mTvComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                comment();
            }
        });

        mTvDeleteOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteOrder();
            }
        });

        mTvPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pay();
            }
        });
        mTvAfterSaleAbove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                afterSale();
            }
        });

    }

    private void pay() {
        Intent intent = new Intent(mContext, AtyPay.class);
        intent.putExtra(MyConstants.ORDER_ID, mOrderID);
        startActivityForResult(intent,REQUEST_CODE);
    }

    HttpOnNextListener<BaseResultEntity> mDeleteOrderListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            CustomToastResult.show("删除成功");
            finish();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void deleteOrder() {
        final Dialog dialog = new Dialog(mContext, R.style.MyDialog);
        dialog.setContentView(R.layout.app_dialog_sure_cancel);
        TextView tvContent = dialog.findViewById(R.id.tvAppDIalogSureCancelContent);
        TextView tvCancel = dialog.findViewById(R.id.tvAppDialogSureCancelCancel);
        TextView tvSure = dialog.findViewById(R.id.tvAppDialogSureCancelSure);
        tvContent.setText("确定要删除订单吗?");
        tvCancel.setText("取消");
        tvSure.setText("确定");
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ApiDeleteOrder apiDeleteOrder = new ApiDeleteOrder(mDeleteOrderListener, mContext, true, false, false);
                Map<String, Object> map = new HashMap<>();
                map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
                map.put("ORDERS_ID", mOrderID);
                apiDeleteOrder.setMap(map);
                HttpManager.getInstance().doHttpDeal(apiDeleteOrder);
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.show();

    }

    private void comment() {
        Intent intent = new Intent(mContext, AtyCommentCenter.class);
        startActivityForResult(intent,REQUEST_CODE);
    }

    private void afterSale() {
        Intent intent = new Intent(mContext, AtyAfterSale.class);
        startActivityForResult(intent,REQUEST_CODE);
    }

    HttpOnNextListener<BaseResultEntity> mConfirmReceiveListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            CustomToastResult.show("确认成功");
            finish();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void confirmReceive() {
        final Dialog dialog = new Dialog(mContext, R.style.MyDialog);
        dialog.setContentView(R.layout.app_dialog_sure_cancel);
        TextView tvContent = dialog.findViewById(R.id.tvAppDIalogSureCancelContent);
        TextView tvCancel = dialog.findViewById(R.id.tvAppDialogSureCancelCancel);
        TextView tvSure = dialog.findViewById(R.id.tvAppDialogSureCancelSure);
        tvContent.setText("如确定已收到货，请确认收货，如未收到货，请谨慎确认。");
        tvCancel.setText("取消");
        tvSure.setText("确定");
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ApiConfirmReceive apiConfirmReceive = new ApiConfirmReceive(mConfirmReceiveListener, mContext, true, false, false);
                Map<String, Object> map = new HashMap<>();
                map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
                map.put("ORDERS_ID", mOrderID);
                apiConfirmReceive.setMap(map);
                HttpManager.getInstance().doHttpDeal(apiConfirmReceive);
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });

        dialog.show();

    }

    private void checkExpress() {
        Intent intent = new Intent(mContext, AtyCheckExpress.class);
        intent.putExtra(MyConstants.ORDER_ID, mOrderID);
        startActivity(intent);
    }

    HttpOnNextListener<BaseResultEntity> mNoticeSendListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            CustomToastResult.show("提醒成功");
            mTvRemindSend.setText("已提醒");
            mTvRemindSend.setClickable(false);
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void noticeSend() {
        ApiNoticeSendGoods apiNoticeSendGoods = new ApiNoticeSendGoods(mNoticeSendListener, mContext, true, false, false);
        Map<String, Object> map = new HashMap<>();
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiNoticeSendGoods.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiNoticeSendGoods);
    }

    HttpOnNextListener<BaseResultEntity> mCancelOrderListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            CustomToastResult.show("取消成功");
            finish();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void cancelOrder() {
        ApiCancelOrder apiCancelOrder = new ApiCancelOrder(mCancelOrderListener, mContext, true, false, false);
        Map<String, Object> map = new HashMap<>();
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        map.put("ORDERS_ID", mOrderID);
        apiCancelOrder.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiCancelOrder);
    }

    private void contactService() {
        Intent intent = new Intent(mContext, AtyContactUs.class);
        startActivity(intent);
    }

    private BeanOrder mBeanOrder;
    HttpOnNextListener<BeanOrder> mGetOrderDetailListener = new HttpOnNextListener<BeanOrder>() {
        @Override
        public void onNext(BeanOrder beanOrder) {
            mBeanOrder = beanOrder;
            mAdpChild.setNewData(beanOrder.getGOODS());
            bindViewWidthData();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void bindViewWidthData() {
        mTvOrderNum.setText("订单号: " + mBeanOrder.getORDERS_ID());
        mTvReceiveName.setText(mBeanOrder.getCUSTOMERNAME());
        mTvReceivePhone.setText(mBeanOrder.getCUSTOMERPHONE());
        mTvAddress.setText(mBeanOrder.getCUSTOMERPROVINCE() + mBeanOrder.getCUSTOMERCITY() + mBeanOrder.getCUSTOMERAREA() + mBeanOrder.getCUSTOMERADDRESS());
        mTvTotalMoney.setText("¥  " + mBeanOrder.getORDERMONEY());
        mTvCouponMoney.setText("¥  " + mBeanOrder.getCOUPONMONEY());
        mTvNeedMoney.setText("¥  " + mBeanOrder.getNEEDPAYMONEY());
        mTvOrderStatus.setTextColor(mContext.getResources().getColor(R.color.color_43b624));
        String payWay = null;
        if (mBeanOrder.getPAYTYPE() != null) {
            payWay = "";
            switch (mBeanOrder.getPAYTYPE()) {
                case "0":
                    payWay = "支付宝";
                    break;
                case "1":
                    payWay = "微信";
                    break;
                case "2":
                    payWay = "线下";
                    break;
                default:
                    break;
            }
        }

        Date deadDate = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
             deadDate = format.parse(mBeanOrder.getDEADLINE());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date currentDate = new Date(System.currentTimeMillis());
        String datePoor = TimeUtils.getDatePoor(deadDate, currentDate);

        switch (mBeanOrder.getSTATUS()) {
            case 0://待付款
                mRlPayWay.setVisibility(View.GONE);
                mRlCheckExpress.setVisibility(View.GONE);
                mTvOrderStatus.setText("待付款");
                mRlRestTime.setVisibility(View.VISIBLE);
                mTvRestTime.setText("剩" + datePoor + "自动关闭");

                mTvService.setVisibility(View.VISIBLE);
                mTvPay.setVisibility(View.VISIBLE);
                mTvCancelOrder.setVisibility(View.VISIBLE);

                mTvRemindSend.setVisibility(View.GONE);
                mTvCheckExpress.setVisibility(View.GONE);
                mTvConfirmReceive.setVisibility(View.GONE);
                mTvAfterSale.setVisibility(View.GONE);
                mTvComment.setVisibility(View.GONE);
                mTvDeleteOrder.setVisibility(View.GONE);


                break;
            case 1://待发货
                mRlPayWay.setVisibility(View.VISIBLE);
                mTvPayWay.setText("支付方式: " + payWay);
                mRlCheckExpress.setVisibility(View.GONE);
                mRlRestTime.setVisibility(View.GONE);

                mTvOrderStatus.setText("待发货");
                mTvService.setVisibility(View.VISIBLE);
                mTvRemindSend.setVisibility(View.VISIBLE);
                if ("1".equals(mBeanOrder.getISREMIND())){
                    mTvRemindSend.setText("已提醒");
                    mTvRemindSend.setClickable(false);
                }
                mTvCancelOrder.setVisibility(View.VISIBLE);

                mTvCheckExpress.setVisibility(View.GONE);
                mTvConfirmReceive.setVisibility(View.GONE);
                mTvAfterSale.setVisibility(View.GONE);
                mTvComment.setVisibility(View.GONE);
                mTvDeleteOrder.setVisibility(View.GONE);
                mTvPay.setVisibility(View.GONE);
                break;
            case 2://已发货
                mRlPayWay.setVisibility(View.VISIBLE);
                mTvPayWay.setText("支付方式: " + payWay);
                mRlCheckExpress.setVisibility(View.VISIBLE);
                mRlRestTime.setVisibility(View.GONE);

                mTvOrderStatus.setText("已发货");
                mTvService.setVisibility(View.VISIBLE);
                mTvCheckExpress.setVisibility(View.VISIBLE);
                mTvConfirmReceive.setVisibility(View.VISIBLE);

                mTvRemindSend.setVisibility(View.GONE);
                mTvCancelOrder.setVisibility(View.GONE);
                mTvAfterSale.setVisibility(View.GONE);
                mTvComment.setVisibility(View.GONE);
                mTvDeleteOrder.setVisibility(View.GONE);
                mTvPay.setVisibility(View.GONE);
                break;
            case 3://已完成
                mRlPayWay.setVisibility(View.VISIBLE);
                mTvPayWay.setText("支付方式: " + payWay);
                mRlCheckExpress.setVisibility(View.VISIBLE);
                mRlRestTime.setVisibility(View.GONE);

                mTvOrderStatus.setText("已完成");
                mTvDeleteOrder.setVisibility(View.VISIBLE);

                mTvAfterSale.setVisibility(View.GONE);
                mTvService.setVisibility(View.GONE);
                mTvCheckExpress.setVisibility(View.GONE);
                mTvConfirmReceive.setVisibility(View.GONE);
                mTvRemindSend.setVisibility(View.GONE);
                mTvCancelOrder.setVisibility(View.GONE);
                mTvComment.setVisibility(View.GONE);
                mTvPay.setVisibility(View.GONE);
                break;
            case 4://已取消
                mRlPayWay.setVisibility(View.GONE);
                mRlCheckExpress.setVisibility(View.GONE);
                mRlRestTime.setVisibility(View.GONE);
                mTvOrderStatus.setTextColor(mContext.getResources().getColor(R.color.color_999999));

                mTvOrderStatus.setText("已取消");
                mTvDeleteOrder.setVisibility(View.VISIBLE);

                mTvAfterSale.setVisibility(View.GONE);
                mTvService.setVisibility(View.GONE);
                mTvCheckExpress.setVisibility(View.GONE);
                mTvConfirmReceive.setVisibility(View.GONE);
                mTvRemindSend.setVisibility(View.GONE);
                mTvCancelOrder.setVisibility(View.GONE);
                mTvComment.setVisibility(View.GONE);
                mTvPay.setVisibility(View.GONE);

                break;
            case 5://待评价
                mRlPayWay.setVisibility(View.VISIBLE);
                mTvPayWay.setText("支付方式: " + payWay);
                mRlCheckExpress.setVisibility(View.VISIBLE);

                mRlRestTime.setVisibility(View.GONE);
                mTvOrderStatus.setText("待评价");
                mTvComment.setVisibility(View.VISIBLE);
                mTvAfterSaleAbove.setVisibility(View.VISIBLE);

                mTvDeleteOrder.setVisibility(View.GONE);
                mTvAfterSale.setVisibility(View.GONE);
                mTvService.setVisibility(View.GONE);
                mTvCheckExpress.setVisibility(View.GONE);
                mTvConfirmReceive.setVisibility(View.GONE);
                mTvRemindSend.setVisibility(View.GONE);
                mTvCancelOrder.setVisibility(View.GONE);
                mTvPay.setVisibility(View.GONE);
                break;
            case 6://待收货
                mRlPayWay.setVisibility(View.VISIBLE);
                mTvPayWay.setText("支付方式: " + payWay);
                mRlCheckExpress.setVisibility(View.VISIBLE);
                mRlRestTime.setVisibility(View.GONE);
                mTvOrderStatus.setText("待收货");
                mTvAfterSale.setVisibility(View.VISIBLE);
                mTvService.setVisibility(View.VISIBLE);
                mTvCheckExpress.setVisibility(View.VISIBLE);
                mTvConfirmReceive.setVisibility(View.VISIBLE);

                mTvComment.setVisibility(View.GONE);
                mTvDeleteOrder.setVisibility(View.GONE);
                mTvRemindSend.setVisibility(View.GONE);
                mTvCancelOrder.setVisibility(View.GONE);
                mTvPay.setVisibility(View.GONE);
                break;
        }
    }

    private void getData() {
        ApiGetOrderDetail apiGetOrderDetail = new ApiGetOrderDetail(mGetOrderDetailListener, mContext, true, false, false);
        Map<String, Object> map = new HashMap<>();
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        map.put("ORDERS_ID", mOrderID);
        apiGetOrderDetail.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetOrderDetail);
    }

    private void initRv() {
        ScrollLinearLayoutManager layoutManager = new ScrollLinearLayoutManager(mContext);
        mRvChild.setLayoutManager(layoutManager);
        List<BeanOrder.GOODSBean> goodsList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            BeanOrder.GOODSBean goodsBean = new BeanOrder.GOODSBean();
            goodsList.add(goodsBean);
        }
        mAdpChild = new AdpRvAtyOrderDetailChild(R.layout.app_recycle_item_aty_balance_content, goodsList);
        mRvChild.setAdapter(mAdpChild);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE){
            finish();
        }
    }
}
